import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

class XHAutoHeightTextField extends StatefulWidget {
  final int maxLength;
  final String? placeholder;
  final TextEditingController? controller;
  final double width;
  final double minHeight;
  final double maxHeight;
  final Function(String)? onChanged;
  final TextStyle? textStyle;
  final TextStyle? placeholderStyle;
  final bool numberOnly;

  const XHAutoHeightTextField({
    Key? key,
    required this.maxLength,
    this.placeholder,
    this.controller,
    required this.width,
    this.minHeight = 40,
    this.maxHeight = 120,
    this.onChanged,
    this.textStyle,
    this.placeholderStyle,
    this.numberOnly = false,
  }) : super(key: key);

  @override
  State<XHAutoHeightTextField> createState() => _XHAutoHeightTextFieldState();
}

class _XHAutoHeightTextFieldState extends State<XHAutoHeightTextField> {
  late TextEditingController _controller;
  double _currentHeight = 40;

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? TextEditingController();
    _controller.addListener(_onTextChanged);
  }

  @override
  void dispose() {
    if (widget.controller == null) {
      _controller.dispose();
    }
    super.dispose();
  }

  void _onTextChanged() {
    if (_controller.text.length > widget.maxLength) {
      _controller.text = _controller.text.substring(0, widget.maxLength);
      _controller.selection = TextSelection.fromPosition(
        TextPosition(offset: widget.maxLength),
      );
    }
    _updateHeight();
    widget.onChanged?.call(_controller.text);
  }

  void _updateHeight() {
    final textStyle = widget.textStyle ?? const TextStyle(fontSize: 15);
    final textSpan = TextSpan(
      text: _controller.text,
      style: textStyle,
    );

    final textPainter = TextPainter(
      text: textSpan,
      textDirection: TextDirection.ltr,
      maxLines: null,
    )..layout(maxWidth: widget.width - 20); // 减去左右padding

    setState(() {
      _currentHeight = textPainter.height; // 加上上下padding
      if (_currentHeight < widget.minHeight) {
        _currentHeight = widget.minHeight;
      }
      if (_currentHeight > widget.maxHeight) {
        _currentHeight = widget.maxHeight;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: _currentHeight,
      decoration: BoxDecoration(
        color: CupertinoColors.systemBackground,
        borderRadius: BorderRadius.circular(8),
      ),
      child: CupertinoTextField(
        controller: _controller,
        placeholder: widget.placeholder,
        maxLength: widget.maxLength,
        style: widget.textStyle ?? const TextStyle(fontSize: 16),
        placeholderStyle: widget.placeholderStyle ?? 
            const TextStyle(color: CupertinoColors.systemGrey),
        decoration: null,
        maxLines: null,
        minLines: 1,
        textAlignVertical: TextAlignVertical.center,
        textAlign: TextAlign.end,
        keyboardType: widget.numberOnly ? TextInputType.number : TextInputType.text,
        inputFormatters: widget.numberOnly ? [
          FilteringTextInputFormatter.digitsOnly,
        ] : null,
        onChanged: (value) {
          widget.onChanged?.call(value);
        },
      ),
    );
  }
} 